/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgEdit.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgEdit.h"
#include ".\dlgedit.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgEdit, CRgnDialog)
DlgEdit::DlgEdit(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgEdit::IDD, pParent), vEdt(_T(""))
{
	skinIn = NULL;
	infoString.Empty();
	dataString.Empty();
	retVal = false;
}

DlgEdit::~DlgEdit()
{
}

//---------------------------------------------------------------------------

void DlgEdit::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STA_EDIT, staEdt);
	DDX_Control(pDX, IDC_EDT_EDT, edtEdt);
	DDX_Control(pDX, IDC_BTN_OK, btnOk);
	DDX_Control(pDX, IDC_BTN_CANCEL, btnCancel);
	DDX_Text(pDX, IDC_EDT_EDT, vEdt);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgEdit, CRgnDialog)
	ON_BN_CLICKED(IDC_BTN_OK, OnBnClickedBtnOk)
	ON_BN_CLICKED(IDC_BTN_CANCEL, OnBnClickedBtnCancel)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

void DlgEdit::OnOK()
{
	// CDialog::OnOK();
}

void DlgEdit::OnCancel()
{
	// CDialog::OnCancel();
}

//---------------------------------------------------------------------------

BOOL DlgEdit::OnInitDialog()
{
	CRgnDialog::OnInitDialog();

	staEdt.SetWindowText(infoString);

	vEdt = dataString;
	UpdateData(FALSE);

	SetProperties(skinIn, false, _T(""));

	if(skinIn!=NULL)
		staEdt.textColor = skinIn->textColor;

	return TRUE;
}

//---------------------------------------------------------------------------

void DlgEdit::OnBnClickedBtnOk()
{
	UpdateData(TRUE);
	dataString = vEdt;

	retVal = true;
	EndDialog(IDOK);
}

void DlgEdit::OnBnClickedBtnCancel()
{
	retVal = false;
	EndDialog(IDABORT);
}

//---------------------------------------------------------------------------
void DlgEdit::ResizeChilds(void)
{
	int lh;			///< label's control height
	int ew, eh;		///< edit control's width and height
	int bw, bh;		///< button's width and height
	int tw, th;		///< total size of all the objects (bounding rectangle)
	int cw, ch;		///< client region's width and height
	int mx, my;		///< left and top space for displaying the objects in the center of the window
	int dd;			///< space between objects
	RECT rect;
	int posx, posy;

	dd = 2;

	staEdt.GetWindowRect(&rect);
	lh = rect.bottom - rect.top;

	edtEdt.GetWindowRect(&rect);
	ew = rect.right - rect.left;
	eh = rect.bottom - rect.top;

	btnOk.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	///< tw and th are computed according to the layout
	tw = ew;
	th = lh + dd + eh + dd + bh;

	cw = clientRect.right - clientRect.left;
	ch = clientRect.bottom - clientRect.top;

	mx = dd;
	my = dd;

	posx = clientRect.left + mx;
	posy = clientRect.top + my;

	staEdt.MoveWindow(posx, posy, tw, lh, TRUE);

	posy += lh + dd;
	edtEdt.MoveWindow(posx, posy, tw, eh, TRUE);

	posx += tw - bw;
	posy += eh + dd;
	btnCancel.MoveWindow(posx, posy, bw, bh, TRUE);
	posx -= (bw + dd);
	btnOk.MoveWindow(posx, posy, bw, bh, TRUE);
}

bool DlgEdit::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = 265;
	rectIn->top    = 0;
	rectIn->bottom = 67;

	return true;
}

//---------------------------------------------------------------------------
